/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							 script_random_number.cpp

	$Header: /heroes4/script_random_number.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "script_random_number.h"

#include "sized_int_types.h"
#include "streambuf_operators.h"
#include "random.h"

// --------------------------------------------------------------------------
// t_script_random_number members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_random_number::read_values( std::streambuf& stream )
{
	m_min_value = get< t_int32 >( stream );
	if ( m_min_value < 0 )
		return false;
	m_max_value = get< t_int32 >( stream );
	return m_max_value >= m_min_value;
}

bool t_script_random_number::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	return read_values( stream );
}

bool t_script_random_number::read( std::streambuf& stream, int version )
{
	return read_values( stream );
}

bool t_script_random_number::write( std::streambuf& stream ) const
{
	if (t_abstract_script_numeric_expression::write( stream ) == false) 
		return false;
	
	put< t_int32 >( stream, m_min_value);
	put< t_int32 >( stream, m_max_value);
	
	return true;
}

t_script_random_number::t_result 
t_script_random_number::evaluate(t_expression_context_global const& context) const
{
	return random(get_min_value(), get_max_value());
}



